<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_UserGroup extends Controller {
    
    public function action_saveInfo(){

        $id=$this->request->post('model_id');

        $caption=$this->request->post('caption');

        $remark=$this->request->post('remark');

        $model=new Model_Admin_Users();

        echo $model->saveInfo($id,$caption,$remark);
    }
    public function action_deleteInfo(){

        $id=$this->request->post('id');

        $model=new Model_Admin_Common();

        echo $model->deleteInfo("users",$id);
    }
    public function action_loadInfoById(){

        $id=$this->request->post('id');

        $sql="select t.usersid model_id,t.caption,t.remark
              from users t
              where t.usersid='$id'
              limit 1";

        $model=new Model_Admin_Common();
        
        echo $model->loadInfoById($sql);
    }
    public function action_setUser(){

        $usersid=$this->request->query("usersid");

        $caption=$this->request->query("caption");

        $tag=$this->request->query("tag");

        $name=$this->request->query('name');

        $email=$this->request->query('email');

        $url='/UserGroup/setUser?usersid='+$usersid+"&caption="+$caption;

        if(!empty($tag)){

            $url=$url."&tag="+$tag;

        }

        if(!empty($name)){

            $url=$url."&name="+$name;

        }

        if(!empty($email)){

            $url=$url."&email="+$email;

        }

        $model=new Model_Admin_SysUser();

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $userList=$model->getUserByUsersid($tag,$usersid,$name,$email,$offset,$pageSize);

        $pages=ceil((intval($userList['total']))/$pageSize);

        $view = View::factory('admin/sys/setGroupUser');

        $view->userList =$userList;

        $view->page= $page;

        $view->pages=$pages;

        $view->name=$name;

        $view->email=$email;

        $view->tag=$tag;

        $view->url=$url;

        $view->usersid=$usersid;

        $view->caption=$caption;

        $this->response->body($view);
    }

    public function action_addUser(){

        $usersid=$this->request->post("usersid");

        $id=$this->request->post("id");

        $model=new Model_Admin_Users();

        echo $model->addUser($usersid,$id);
    }
    public function action_removeUser(){

        $usersid=$this->request->post("usersid");

        $id=$this->request->post("id");

        $model=new Model_Admin_Users();

        echo $model->removeUser($usersid,$id);
    }
    public function action_setModel(){

        $usersid=$this->request->query("usersid");

        $caption=$this->request->query("caption");

        $tag=$this->request->query("tag");

        $name=$this->request->query('name');

        $url='/UserGroup/setModel?usersid='+$usersid+"&caption="+$caption;

        if(!empty($tag)){

            $url=$url."&tag="+$tag;

        }
        if(!empty($name)){
            
            $url=$url."&name="+$name;
        }

        $model=new Model_Admin_SysModel();

        $page=$this->request->query("page");

        $pageSize=$this->request->query("pageSize");

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);

        $userList=$model->getModelByUsersid($tag,$usersid,$name,$offset,$pageSize);

        $pages=ceil((intval($userList['total']))/$pageSize);

        $view = View::factory('admin/sys/setGroupModel');

        $view->userList =$userList;

        $view->page= $page;

        $view->pages=$pages;

        $view->name=$name;

        $view->tag=$tag;

        $view->usersid=$usersid;

        $view->url=$url;

        $view->caption=$caption;

        $this->response->body($view);
    }

    public function action_addModel(){

        $usersid=$this->request->post("usersid");

        $id=$this->request->post("id");

        $model=new Model_Admin_Users();

        echo $model->addModel($usersid,$id);
    }
    public function action_removeModel(){

        $usersid=$this->request->post("usersid");

        $id=$this->request->post("id");

        $model=new Model_Admin_Users();

        echo $model->removeModel($usersid,$id);
    }

    public function action_setLimit(){

        $data=$this->request->post("data");

        $data_list=json_decode($data,true);

        foreach ($data_list as $key => $list) {

            $sql="update user_model set look='".$list['look']."',`add`='".$list['add']."',edit='".$list['edit']."',remove='".$list['remove']."' where user_modelid='".$list['id']."';";
            
            DB::query(null,$sql)->execute();
        }

        echo json_encode(array("info"=>"success"));
    }
}
?>